package nickthissen.iracing.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nickthissen.iracing.datamodels.Page;
import nickthissen.iracing.iracingforum2.R;

/**
 * Created by nthissen on 17/05/13.
 */
public class PagerAdapter extends ArrayAdapter<Page>
{
    private ArrayList<Page> pages;

    public PagerAdapter(Context context, int textViewResourceId, ArrayList<Page> items)
    {
        super(context, textViewResourceId, items);
        pages = items;
    }

    private View getCustomView(View convertView, int resourceId, String text)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(resourceId, null);
        }

        TextView txt = (TextView) v.findViewById(R.id.txt);
        txt.setText(text);

        return v;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        Page page = pages.get(position);
        if (page != null)
        {
            return getCustomView(convertView, R.layout.page_dropdown, Integer.toString(page.Number));

        }
        return null;
    }

    @Override
    public int getCount()
    {
        if (pages == null) return 0;
        return pages.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        Page page = pages.get(position);
        if (page != null)
        {
            return getCustomView(convertView, R.layout.page_display, page.Number + " / " + page.TotalPages);
        }
        return null;
    }
}
